package cn.net.xinyi.xs.view {
	
	import cn.net.xinyi.xs.RamsFacade;
	import cn.net.xinyi.xs.model.LoadChartsProxy;
	import cn.net.xinyi.xs.view.components.ColumnChart;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class ColumnChartMediator extends Mediator {
		
		public static const NAME:String = 'ColumnChartMediator';
		
		public function ColumnChartMediator(viewComponent:ColumnChart) {
			super(NAME, viewComponent);
			charts.addEventListener(ColumnChart.LOAD_CHARTS, onLoadCharts);
		}
		
		protected function onLoadCharts(event:Event):void {
			sendNotification(RamsFacade.LOAD_CHARTS);
		}
		
		override public function listNotificationInterests():Array {
			return [
				LoadChartsProxy.LOAD_CHARTS_SUCCESS,
				LoadChartsProxy.LOAD_CHARTS_FAILED
			];
		}
		
		override public function handleNotification(note:INotification):void {
			switch (note.getName()) {
				case LoadChartsProxy.LOAD_CHARTS_SUCCESS:
					 charts.chart.dataProvider = note.getBody() as ArrayCollection;
					 break;
				case LoadChartsProxy.LOAD_CHARTS_FAILED:
					 trace(note.getBody().toString());
					 Alert.show("服务器响应错误，请与管理员联系！", "Error");
					 break;
			}
		}
		
		protected function get charts():ColumnChart {
			return viewComponent as ColumnChart;
		}
	}
}